package com.yuhq.controller;

import com.alibaba.fastjson.JSON;
import com.yuhq.model.ProjectInfo;
import com.yuhq.service.ProjectInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目信息控制器
 *
 * @author sam
 */
@Controller
public class ProjectInfoController {

    private static Logger LOG = Logger.getLogger(ProjectInfoController.class);

    @Resource
    private ProjectInfoService projectInfoService;

    @RequestMapping("/index")
    public String index(Integer status, String resultMsg, Model model) {
        LOG.info(" exec ProjectInfoController -> index params: status=" + status);
        //根据状态查询项目列表
        List<ProjectInfo> projectInfoList = projectInfoService.findByStatus(status);
        //传递参数到view
        model.addAttribute("projectInfoList", projectInfoList);
        model.addAttribute("status", status);
        model.addAttribute("resultMsg", resultMsg);

        LOG.info(" exec ProjectInfoController -> index result:" +
                JSON.toJSONString(projectInfoList));
        return "index";
    }

    @RequestMapping("/show")
    public String show(Long id, Model model) {
        LOG.info(" exec ProjectInfoController -> show params: id=" + id);
        ProjectInfo projectInfo = projectInfoService.findById(id);
        LOG.info(" exec ProjectInfoController -> show result: projectInfo=" + projectInfo);
        model.addAttribute("projectInfo", projectInfo);
        return "show";
    }

    @RequestMapping("/modifyStatus")
    public String modifyStatus(Long id, Integer status) {
        LOG.info(" exec ProjectInfoController -> modifyStatus params: id=" + id + " status=" + status);
        Integer result = projectInfoService.modifyStatus(id, status);
        LOG.info(" exec ProjectInfoController -> modifyStatus result: 响应条数=" + result);
        String resultMsg = "审核成功";
        if (result <= 0) {
            resultMsg = "审核失败";
        }
        //redirect: 重定向
        return "redirect:/index?resultMsg=" + resultMsg + "&status=" + status;
    }

}
